home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Help with a very weird C problem
- Date: Fri, 05 Apr 96 15:30:59 GMT
- Organization: none
- Message-ID: <828718259snz@genesis.demon.co.uk>
- References: <31620c87.184254741@199.171.83.2> <4k2tdl$ft4@preeda.internex.net.au>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4k2tdl$ft4@preeda.internex.net.au>
- sultan@connexus.apana.org.au "Jon Hornstein" writes:
-
- >With the briefest of looks at the code this function
- >
- >
- >>char *PkunzipDir(char *curdir,char *name2,char *file)
- >>{
- >>char pkunzip2[120] = "pkunzip ";
- >>strcat(pkunzip2,curdir);
- >>strcat(pkunzip2,name2);
- >>strcat(pkunzip2," ");
- >>strcat(pkunzip2,curdir);
- >>strcat(pkunzip2,file);
- >>return(pkunzip2);
- >>}
- >
- >should read
- >
- >
- >char *PkunzipDir(char *curdir,char *name2,char *file)
- >{
- >static char pkunzip2[120] = "pkunzip ";
- >
- > strcat(pkunzip2,curdir);
- > strcat(pkunzip2,name2);
- > strcat(pkunzip2," ");
- > strcat(pkunzip2,curdir);
- > strcat(pkunzip2,file);
- > return(pkunzip2);
- >}
-
- That would fail if you called PkunzipDir() more than once.
-
- Or you could write it more simply and clearly (and correctly) as:
-
- char *PkunzipDir(char *curdir,char *name2,char *file)
- {
- static char pkunzip2[120];
-
- sprintf(pkunzip2, "pkunzip %s%s %s%s", curdir, name2, curdir, file);
-
- return pkunzip2;
- }
-
- sprintf is extremely useful for string handling.
-
- It is generally better to have the calling function pass a pointer to a
- suitable buffer than to return a pointer to a static buffer.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-